package com.vote.admin.service;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.vote.admin.vo.param.SwitchParam;
import com.vote.admin.vo.result.RecordResult;
import com.vote.admin.vo.result.UserResult;
import com.vote.common.entity.VoteOption;
import com.vote.common.entity.VoteProject;
import com.vote.common.entity.VoteRecord;
import com.vote.common.entity.VoteUser;
import com.vote.common.enums.UserTypeEnum;
import com.vote.common.enums.VoteChoiceEnum;
import com.vote.common.enums.VoteTypeEnum;
import com.vote.common.returns.RespCode;
import com.vote.common.returns.RespEntity;
import com.vote.common.returns.RespTable;
import com.vote.common.service.VoteOptionService;
import com.vote.common.service.VoteProjectService;
import com.vote.common.service.VoteRecordService;
import com.vote.common.service.VoteUserService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class AdminRecordService {

    @Autowired
    private VoteRecordService voteRecordService;

    @Autowired
    private VoteUserService voteUserService;

    @Autowired
    private VoteProjectService voteProjectService;

    @Autowired
    private VoteOptionService voteOptionService;

    public RespTable<List<RecordResult>> voteRecordList(Integer page, Integer limit) {
        QueryWrapper<VoteRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(VoteRecord::getDeleted, 0)
                .orderByDesc(VoteRecord::getId);

        IPage<VoteRecord> iPage = new Page<>(page, limit);
        IPage<VoteRecord> pg = voteRecordService.page(iPage, queryWrapper);

        List<VoteRecord> records = pg.getRecords();
        List<RecordResult> recordResultList = Lists.newArrayList();
        if (CollectionUtil.isNotEmpty(records)) {
            for (VoteRecord record : records) {
                //用户信息
                String username = UserTypeEnum.NO_USERNAME.getDesc();
                String name = UserTypeEnum.NO_NAME.getDesc();
                String type = UserTypeEnum.NO_NAME.getDesc();
                if (Objects.nonNull(record.getUserId())) {
                    VoteUser voteUser = voteUserService.getById(record.getUserId());
                    username = voteUser.getUsername();
                    name = voteUser.getName();
                    type = UserTypeEnum.getInstance(voteUser.getType()).getDesc();
                }
                //活动信息
                VoteProject voteProject = voteProjectService.getById(record.getProjectId());
                //选项信息
                VoteOption voteOption = voteOptionService.getById(record.getOptionId());

                //数据组装
                RecordResult recordResult = RecordResult.builder()
                        .id(record.getId())
                        .username(username)
                        .name(name)
                        .type(type)
                        .projectName(voteProject.getProjectName())
                        .projectImg(voteProject.getProjectImg())
                        .projectPublic(VoteTypeEnum.getInstance(voteProject.getProjectPublic()).getType())
                        .projectChoice(VoteChoiceEnum.getInstance(voteProject.getProjectChoice()).getDesc())
                        .optionName(voteOption.getOptionName())
                        .optionImg(voteOption.getOptionImg())
                        .recordTime(record.getRecordTime())
                        .build();

                recordResultList.add(recordResult);
            }
        }

        return new RespTable<>(RespCode.OK, recordResultList, pg.getTotal());
    }


}
